<template>
  <el-container class="subject">
    <!-- 头部区域 -->
    <el-header>
      <div class="top-zuo">
        <img src="../assets/heima.png" alt="" />
        <span>电商后台管理页面</span>
      </div>
      <el-button type="info" plain @click="signOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左侧区域 -->
      <el-aside :width="iszedie ? '64px' : '200px'">
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409BFF"
          unique-opened
          :collapse="iszedie"
          :collapse-transition="false"
          router
          :default-active="active"
        >
          <div class="zedie" @click="zedie">
            {{ iszedie ? "三" : "|||" }}
          </div>
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            :key="item.id"
            v-for="item in menulist"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="elicon[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/home/' + val.path"
              :key="val.id"
              v-for="val in item.children"
              @click="save('/home/' + val.path)"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ val.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 页面显示区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      elicon: {
        125: "el-icon-user-solid",
        103: "el-icon-s-tools",
        101: "el-icon-s-goods",
        102: "el-icon-s-order",
        145: "el-icon-s-marketing",
      },
      iszedie: true,
      // 被激活的链接地址
      active: "",
    };
  },
  created() {
    this.getMenuList();
    this.active = window.sessionStorage.getItem("active");
  },
  methods: {
    zedie() {
      this.iszedie = !this.iszedie;
    },
    signOut() {
      // 清空token值
      window.sessionStorage.clear();
      //   返回登录页面
      this.$router.push("/login");
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menulist = res.data;
    },
    // 保存链接的激活状态
    save(active) {
      window.sessionStorage.setItem("active", active);
      this.active = active;
    },
  },
};
</script>
<style lang="less" scoped>
.subject {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  .top-zuo {
    img {
      width: 56px;
      vertical-align: middle;
    }
    span {
      padding: 15px;
      font-size: 23px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border: 0;
    .zedie {
      background-color: #4a5064;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.3em;
      cursor: pointer;
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>